package com.lanju.common;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.http.client.ClientProtocolException;

import com.lanju.controller.WeiXinInterfaceController;
import com.lanju.util.WeiXinUtil;

public class WxMessageUtil {

	static WeiXinInterfaceController weixinInf=new WeiXinInterfaceController();
	/**
	 * 客户交钱后，发送提醒信息
	 * @param req
	 * @param resp
	 * @param templateId
	 * @param userName
	 * @param address
	 * @param pay
	 * @param remark
	 * @param first
	 * @param openid
	 * @return
	 */
	public static Map<String,Object> sendPayMessage(HttpServletRequest req,
			HttpServletResponse resp,String templateId,String userName,String address,String pay,String remark,String first,String openid){
			Map<String, Object> map = new HashMap<>();
			String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
					+ Contants.ACCESS_TOKEN; 
			String data="{\"touser\":\""+openid+"\",\"template_id\":\""+templateId+"\",\"topcolor\":\"#FF0000\", "
					+ "\"data\":{\"first\": { \"value\":\""+first+"\",\"color\":\"#173177\"},"
							+ "\"userName\":{\"value\":\""+userName+"\",\"color\":\"#173177\" },"
									+ "\"address\": { \"value\":\""+address+"\",\"color\":\"#173177\"},"
											+ "\"pay\":{\"value\":\""+pay+"\",\"color\":\"#173177\"}, "
													+ "\"remark\":{\"value\":\""+remark+"\",\"color\":\"#173177\"}"
															+ "}}";
			Date savejsapiTime;
			String jsapiticket = Contants.JSAPI_TICKET;
			try {
				if (jsapiticket != null && jsapiticket != "") {
					// 接下来判断jsapiticket是否超时，如果超时则重新申请一个token再获取一个ticket
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
					savejsapiTime = sdf.parse(Contants.JSAPI_TICKET_TIME);
					savejsapiTime.setHours(savejsapiTime.getHours() + 2);
					Date curdate = new Date();
					int result = curdate.compareTo(savejsapiTime);// 1：还没超时，-1超时
					if (result == -1 || result == 0) {// ticket未超时
						System.out.println("未超时");
					} else if (result == 1) {// ticket超时
						weixinInf.getAccessToken();
					}
				} else {
					weixinInf.getAccessToken();
				}
			} catch (ClientProtocolException e1) {
				e1.printStackTrace();
			} catch (ParseException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			try {
				JSONObject obj = WeiXinUtil.doPostStr(url, data);
				map.put("rows", obj);
				map.put("list", templateId);
				map.put("success", true);
				map.put("msg", "发送成功");
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				map.put("success", false);
				map.put("msg", "发送失败");
			} catch (IOException e) {
				e.printStackTrace();
				map.put("success", false);
				map.put("msg", "发送失败");
			}
				return map;
	}
	/**
	 * 发送合同统一接口
	 * 作者：zengqd
	 * @param req
	 * @param resp
	 * @param templateId
	 * @param userName
	 * @param address
	 * @param pay
	 * @param remark
	 * @param first
	 * @param openid
	 * @return
	 */
	public static Map<String,Object> sendcontractPayMessage(HttpServletRequest req,
			HttpServletResponse resp,String data){
			Map<String, Object> map = new HashMap<>();
			String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
					+ Contants.ACCESS_TOKEN; 
			Date savejsapiTime;
			String jsapiticket = Contants.JSAPI_TICKET;
			try {
				if (jsapiticket != null && jsapiticket != "") {
					// 接下来判断jsapiticket是否超时，如果超时则重新申请一个token再获取一个ticket
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
					savejsapiTime = sdf.parse(Contants.JSAPI_TICKET_TIME);
					savejsapiTime.setHours(savejsapiTime.getHours() + 2);
					Date curdate = new Date();
					int result = curdate.compareTo(savejsapiTime);// 1：还没超时，-1超时
					if (result == -1 || result == 0) {// ticket未超时
						System.out.println("未超时");
					} else if (result == 1) {// ticket超时
						weixinInf.getAccessToken();
					}
				} else {
					weixinInf.getAccessToken();
				}
			} catch (ClientProtocolException e1) {
				e1.printStackTrace();
			} catch (ParseException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			try {
				JSONObject obj = WeiXinUtil.doPostStr(url, data);
				map.put("rows", obj);
				map.put("success", true);
				map.put("msg", "发送成功");
			} catch (ClientProtocolException e) {
				e.printStackTrace();
				map.put("success", false);
				map.put("msg", "发送失败");
			} catch (IOException e) {
				e.printStackTrace();
				map.put("success", false);
				map.put("msg", "发送失败");
			}
				return map;
	}
	
	/**
	 * 发送合同统一接口
	 * 作者：zengqd
	 * @param req
	 * @param resp
	 * @param templateId
	 * @param userName
	 * @param address
	 * @param pay
	 * @param remark
	 * @param first
	 * @param openid
	 * @return
	 */
	public static Map<String,Object> contractPayMesgJob(String data) throws ClientProtocolException, IOException{
		Map<String, Object> map = new HashMap<>();
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
				+ Contants.ACCESS_TOKEN; 
		try {
			JSONObject obj = WeiXinUtil.doPostStr(url, data);
			if(obj.getString("errmsg").equals("ok")){
				map.put("rows", obj);
				map.put("success", true);
				map.put("msg", "发送成功");
			}else{
				map.put("success", false);
				map.put("msg", "发送失败,原因是： "+obj.getString("errcode"));
			}
			System.out.println(obj);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("msg", "发送失败");
		} catch (IOException e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("msg", "发送失败");
		}
			return map;
	}
}
